package com.hlhj.funlike_business.activity.adapter;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.AddCommentyAty;
import com.hlhj.funlike_business.activity.CameraActivity;
import com.hlhj.funlike_business.activity.PhotoAty;
import com.hlhj.funlike_business.utils.AbulmCameraDialog;
import com.hlhj.funlike_business.utils.MyGlideEngin;
import com.huantansheng.easyphotos.EasyPhotos;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class FoodAdp extends RecyclerView.Adapter<FoodAdp.ViewHolder> {

    private Context context;
    private List<String> strings;
    private AbulmCameraDialog cameraDialog;

    public FoodAdp(Context context, List<String> strings) {
        this.context = context;
        this.strings = strings;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context,R.layout.adp_food,null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        cameraDialog=new AbulmCameraDialog(context);
        if (null != strings && position < strings.size()) {
            Glide.with(context).load(strings.get(position)).into(holder.img_foodphoto);
            holder.tv_del.setVisibility(View.VISIBLE);
            holder.img_foodphoto.setVisibility(View.VISIBLE);
            holder.tv_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if (listener!=null){
//                        listener.showDel(position);
//                    }
                    strings.remove(position);
                    notifyDataSetChanged();
                }
            });
        } else {
            holder.tv_del.setVisibility(View.GONE);
            holder.img_foodphoto.setVisibility(View.GONE);
        }

        holder.tv_addphoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cameraDialog.showDialog();
            }
        });

        cameraDialog.setOnItemClickListener(new AbulmCameraDialog.OnItemClickListener() {
            @Override
            public void onItemListener(int choose) {
                if (choose == cameraDialog.TAKEPHOTO) {
                    Intent intent = new Intent(context, CameraActivity.class);
                    intent.putExtra("type","3");
                    context.startActivity(intent);
                    cameraDialog.dismiss();
                } else if (choose == cameraDialog.AUBLUM) {
                    RxPermissions rxPermissions = new RxPermissions((AddCommentyAty)context);
                    if (!rxPermissions.isGranted(Manifest.permission.CAMERA)) {
                        rxPermissions.request(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO)
                                .subscribe(new Observer<Boolean>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {

                                    }

                                    @Override
                                    public void onNext(Boolean aBoolean) {
                                        if (aBoolean) {
                                            EasyPhotos.createAlbum((AddCommentyAty)context, true, new MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                                                    .setCount(1)
                                                    .setFileProviderAuthority("com.hlhj.funlike_business.cameraProvider")//参数说明：见下方`FileProvider的配置`
                                                    .start(AddCommentyAty.TAKE_PHOTO);
                                        } else {
                                        }


                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onComplete() {

                                    }
                                });
                    } else {
                        EasyPhotos.createAlbum((AddCommentyAty)context, true, new MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                                .setCount(1)
                                .setFileProviderAuthority("com.hlhj.funlike_business.cameraProvider")//参数说明：见下方`FileProvider的配置`
                                .start(AddCommentyAty.TAKE_PHOTO);
                    }
                    cameraDialog.dismiss();
                } else if (choose == cameraDialog.CANCLE) {
                    cameraDialog.dismiss();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (strings.size()<4){
            return strings.size()+1;
        }
        return strings.size();
    }

    class ViewHolder extends BaseViewHolder{
        private ImageView img_foodphoto;
        private TextView tv_del,tv_addphoto;
        public ViewHolder(View view) {
            super(view);
            tv_del=view.findViewById(R.id.tv_del);
            tv_addphoto=view.findViewById(R.id.tv_addphoto);
            img_foodphoto=view.findViewById(R.id.img_foodphoto);
        }
    }

    public interface getDel{
        void showDel(int position);
    }

    public FoodPhotoAdp.getDel listener;

    public void setListener(FoodPhotoAdp.getDel listener) {
        this.listener = listener;
    }
}
